package a66weding.com.jiehuntong.pager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import a66weding.com.jiehuntong.MainActivity;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.ElectronicInvitationHomeActivity;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.activity.ToolsDayPlanActivity;
import a66weding.com.jiehuntong.activity.ToolsGiftsActivity;
import a66weding.com.jiehuntong.activity.ToolsWedRegisterActivity;
import a66weding.com.jiehuntong.activity.Tools_WeddingStoryActivity;
import a66weding.com.jiehuntong.activity.toolsbudget.ToolsBudgetAddbudgetActivity;
import a66weding.com.jiehuntong.activity.toolsholiday.ToolsHolidayActivity;
import a66weding.com.jiehuntong.activity.toolsseat.ToolsSeatAddSeatActivity;
import a66weding.com.jiehuntong.activity.toolstask.ToolsWedTaskActivity;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.GuestsseatBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.CacheUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.view.ConfirmLoginDialog;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
/**
 * 工具
 * Created by y on 2016/5/17.
 */
public class ToolsPager extends BasePager implements View.OnClickListener {
    private static File file;
    private View mToolsView;
    private TextView mHometoolsShare;
    private RelativeLayout mGifibLay;
    private RelativeLayout mDayplanLay;
    private RelativeLayout mSeatLay;
    private RelativeLayout mWedcustomLay;
    private RelativeLayout mWedRegistryLay;
    private RelativeLayout mWedbudgetLay;
    private RelativeLayout mEinvitaionLay;
    private RelativeLayout mWedstoryLay;
    private RelativeLayout mWedtaskLay;
    private RelativeLayout mWeddayLay;
    private boolean isLogin;
    private final int PAGER_BASE_REQUEST_LOGIN_CODE  = 4000;
    public ToolsPager(Activity activity) {
        super(activity);
    }

    @Override
    protected View getView() {
        mToolsView = View.inflate(mActivity, R.layout.pager_tools2, null);
        initView(mToolsView);
        isLogin = ((GlobalApplication) mActivity.getApplication()).isLogin();
        //测试结婚预算：
        return mToolsView;
    }

    private void initView(View mToolsView) {
        mHometoolsShare = (TextView) mToolsView.findViewById(R.id.tv_hometools_share);
        mGifibLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_gifib);
        mDayplanLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_dayplan);
        mSeatLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_seat);
        mWedcustomLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_wedcustom);
        mWedRegistryLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_wedregistry);
        mWedbudgetLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_wedbudget);
        mEinvitaionLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_invitation);
        mWedstoryLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_wedstory);
        mWedtaskLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_wedtask);
        mWeddayLay = (RelativeLayout) mToolsView.findViewById(R.id.rlay_tools_weddingday);
        mGifibLay.setOnClickListener(this);
        mDayplanLay.setOnClickListener(this);
        mSeatLay.setOnClickListener(this);
        mWedcustomLay.setOnClickListener(this);
        mWedRegistryLay.setOnClickListener(this);
        mWedbudgetLay.setOnClickListener(this);
        mEinvitaionLay.setOnClickListener(this);
        mWedstoryLay.setOnClickListener(this);
        mWedtaskLay.setOnClickListener(this);
        mWeddayLay.setOnClickListener(this);
        mHometoolsShare.setOnClickListener(this);
    }

    /**
     * 一键分享
     */
    private void showShare() {
        String title = "结婚通";
        String description = "幸福从哪里开始，幸福从这里开始！";
        String url = "http://j.66wedding.com/downLoad.html";
        copyToSD(mActivity);
//        getShareUtils();
        ShareSDK.initSDK(mActivity);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setTitle(title);
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(description);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        String path = android.os.Environment.getExternalStorageDirectory().getPath() + "/" + "pic_share_0214.jpg";
        oks.setImagePath(path);//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment(description);
        oks.setSiteUrl(url);
        // 启动分享GUI
        oks.show(mActivity);
    }

    /**
     * 把raw的图片存到SD卡上
     */
    public static void copyToSD(Context context) {
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            String path = android.os.Environment.getExternalStorageDirectory()
                    .getPath();
            String dbPathAndName = path + "/" + "pic_share_0214.jpg";

            file = new File(path);

            if (file.exists() == false) {

                file.mkdir();
            }
            File dbFile = new File(dbPathAndName);
            if (!dbFile.exists()) {
                is = context.getResources().openRawResource(
                        R.raw.pic_share);
                fos = new FileOutputStream(dbFile);

                byte[] buffer = new byte[8 * 1024];// 8K
                while (is.read(buffer) > 0)// >
                {
                    fos.write(buffer);
                }
            }
        } catch (Exception e) {

        } finally {
            try {
                if (is != null) {
                    is.close();
                }

                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_hometools_share: //分享
                showShare();
                break;
            case R.id.rlay_tools_gifib://礼金记账本
                if (isLogin) {
                    Intent intent1 = new Intent(mActivity, ToolsGiftsActivity.class);
                    mActivity.startActivity(intent1);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 1);//礼金记账本去登录页面请求码是4001
                }
                break;
            case R.id.rlay_tools_dayplan://结婚当日流程
                if (isLogin) {
                    Intent intent2 = new Intent(mActivity, ToolsDayPlanActivity.class);
                    mActivity.startActivity(intent2);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 2);//结婚当日流程去登录页面请求码是4002
                }
                break;

            case R.id.rlay_tools_seat://排座位
                if (isLogin) {
                    Intent intent3 = new Intent(mActivity, ToolsSeatAddSeatActivity.class);
                    mActivity.startActivity(intent3);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 3);//排座位去登录页面请求码是4003
                }
                break;
            case R.id.rlay_tools_wedcustom://婚礼习俗
                Toast.makeText(mActivity, "此功能暂未开启，敬请期待...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.rlay_tools_wedregistry://结婚登记处
                Intent intent5 = new Intent(mActivity, ToolsWedRegisterActivity.class);
                mActivity.startActivity(intent5);
                break;
            case R.id.rlay_tools_wedbudget://婚礼预算
                if (isLogin) {
                    Intent intent6 = new Intent(mActivity, ToolsBudgetAddbudgetActivity.class);
                    mActivity.startActivity(intent6);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 6);//婚礼预算去登录页面请求码是4006
                }
                break;
            case R.id.rlay_tools_invitation: // 电子请柬
                mActivity.startActivity(new Intent(mActivity, ElectronicInvitationHomeActivity.class));
                break;
            case R.id.rlay_tools_wedstory://结婚故事（这里隐藏掉了）
                Intent intent8 = new Intent(mActivity, Tools_WeddingStoryActivity.class);
                mActivity.startActivity(intent8);
                break;
            case R.id.rlay_tools_wedtask://结婚任务（这里隐藏掉了）
                if (isLogin) {
                    Intent intent9 = new Intent(mActivity, ToolsWedTaskActivity.class);
                    mActivity.startActivity(intent9);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 9);//结婚任务去登录页面请求码是4009
                }
                break;
            case R.id.rlay_tools_weddingday://结婚吉日
                Intent intent = new Intent(mActivity, ToolsHolidayActivity.class);
                mActivity.startActivity(intent);
                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        isLogin =((GlobalApplication) mActivity.getApplication()).isLogin();
        if (requestCode == (this.PAGER_BASE_REQUEST_LOGIN_CODE + 1) && resultCode == 200) {//200是登录页面登录成功返回的并且是礼金记账本
            Intent intent1 = new Intent(mActivity, ToolsGiftsActivity.class);
            mActivity.startActivity(intent1);
        } else if (requestCode == (this.PAGER_BASE_REQUEST_LOGIN_CODE + 2) && resultCode == 200) {//200是登录页面登录成功返回的，结婚当日流程
            Intent intent2 = new Intent(mActivity, ToolsDayPlanActivity.class);
            mActivity.startActivity(intent2);
        } else if (requestCode ==( this.PAGER_BASE_REQUEST_LOGIN_CODE + 3 )&& resultCode == 200) {//200是登录页面登录成功返回的  排座位
            Intent intent3 = new Intent(mActivity, ToolsSeatAddSeatActivity.class);
            mActivity.startActivity(intent3);
        } else if (requestCode == (this.PAGER_BASE_REQUEST_LOGIN_CODE + 6 )&& resultCode == 200) {//200是登录页面登录成功返回的  结婚预算
            Intent intent6 = new Intent(mActivity, ToolsBudgetAddbudgetActivity.class);
            mActivity.startActivity(intent6);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void showLoginDialog(Context context, final int requestcoede) {
        final ConfirmLoginDialog confirmDialog = new ConfirmLoginDialog(context, "亲，需要登录哦~", "去登陆", "取消", R.layout.pop_tologin);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmLoginDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                Intent intent = new Intent(mActivity, LoginActivity.class);
                intent.putExtra("isCanBack", true);
                confirmDialog.dismiss();
                mActivity.startActivityForResult(intent, requestcoede);
            }
            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }

}
